package com.lots.service.impl;

import com.lots.context.BaseContext;
import com.lots.domain.dto.DishUploadDto;
import com.lots.domain.entity.DishDetail;
import com.lots.mapper.DishDetailMapper;
import com.lots.mapper.DishMapper;
import com.lots.domain.entity.Dish;
import com.lots.domain.entity.DishCategory;
import com.lots.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishDetailMapper dishDetailMapper;
    @Override
    public List<Dish> getDishList() {
       return dishMapper.getDishList();

    }

    @Override
    public List<DishCategory> getDishCategories() {
        return  dishMapper.getDishCategories();
    }

    /**
     * 根据类别id查询菜品
     * @param id
     * @return
     */
    @Override
    public List<Dish> getDishByCategoryId(Integer id) {
        return dishMapper.getDishByCategoryId(id);
    }

    @Override
    public List<Dish> getTop() {
       return dishMapper.getTopDish();
    }

    @Override
    public List<Dish> getMathTop() {
        // 本月第一天 00:00:00
        LocalDateTime startTime = LocalDate.now()
                .withDayOfMonth(1)
                .atStartOfDay();

        // 本月最后一天 23:59:59
        LocalDateTime endTime = LocalDate.now()
                .withDayOfMonth(LocalDate.now().lengthOfMonth())
                .atTime(23, 59, 59);

        // 格式化为数据库能识别的字符串
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String start = startTime.format(formatter);
        String end = endTime.format(formatter);

        return dishMapper.getMathTopDish(start, end);
    }

    /**
     * 上传菜品信息
     * @param dishUploadDto
     */
    @Transactional
    @Override
    public void upload(DishUploadDto dishUploadDto) {
        Dish dish = Dish.builder()
                .name(dishUploadDto.getName())
                .description(dishUploadDto.getDescription())
                .image(dishUploadDto.getImage())
                .categorieId(dishUploadDto.getCategoryId())
                .star(0)
                .build();
        dishMapper.upload(dish);
        DishDetail dishDetail = DishDetail.builder()
                //获取到菜品插入数据返回的映射id
                .dishId(dish.getId())
                .ingredients(dishUploadDto.getIngredients())
                .steps(dishUploadDto.getSteps())
                //上下文获取用户id
                .userId(BaseContext.getCurrentId())
                .build();
        dishDetailMapper.upload(dishDetail);

    }

    /**
     * 首页根据类别id查询10个
     */
    @Override
    public List<Dish> getDishByCategoryIdTop(Integer id) {

        return dishMapper.getDishByCategoryIdTop(id);
    }
}
